package com.qf.air.search.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.air.search.config.RabbitMQConfig;
import com.qf.air.search.service.ElasticAirService;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.elasticsearch.search.SearchService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/11/9 21:34
 */
@Component
public class AirListener {
    @Autowired
    private ElasticAirService searchService;
    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = RabbitMQConfig.AIR_QUEUE)
    public void consume(String json, Channel channel, Message message) throws IOException {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        switch (routingKey){
            case "air.save":
                searchService.indexAir(objectMapper.readValue(json,Map.class));
                break;
            case "air.delete":
                searchService.deleteAir(Integer.valueOf(json));
                break;
            case "air.update":
                searchService.updateAir(objectMapper.readValue(json,Map.class));
                break;
            default:
                break;
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
